#include "oled.h"
#include "font.h"

/**********************************************
//IIC Start
**********************************************/
void IIC_Start()
{
   OLED_CLK_Set();
   OLED_SDA_Set();

   OLED_SDA_Clr();

   OLED_CLK_Clr();
}

/**********************************************
//IIC Stop
**********************************************/
void IIC_Stop()
{
   OLED_CLK_Clr();
   OLED_SDA_Clr();

   OLED_CLK_Set();
   OLED_SDA_Set();
}

/**********************************************
// í¨1yI2C×ü??D′ò???×??ú
**********************************************/
void Write_IIC_Byte(unsigned char IIC_Byte)
{
    unsigned char i;
    for(i=0;i<8;i++)
    {
        if(IIC_Byte & 0x80)
            OLED_SDA_Set();
        else
            OLED_SDA_Clr();

        OLED_CLK_Set();

        OLED_CLK_Clr();
        IIC_Byte<<=1;
    }
    OLED_SDA_Set();

    OLED_CLK_Set();

    OLED_CLK_Clr();
}

/*********************OLEDD′êy?Y************************************/
void OLED_WrDat(unsigned char IIC_Data)
{
    IIC_Start();
    Write_IIC_Byte(0x78);
    Write_IIC_Byte(0x40);           //write data
    Write_IIC_Byte(IIC_Data);
    IIC_Stop();
}
/*********************OLEDD′?üá?************************************/
void OLED_WrCmd(unsigned char IIC_Command)
{
    IIC_Start();
    Write_IIC_Byte(0x78);            //Slave address,SA0=0
    Write_IIC_Byte(0x00);           //write command
    Write_IIC_Byte(IIC_Command);
    IIC_Stop();
}
/*********************OLED éè??×?±ê************************************/
void OLED_Set_Pos(unsigned char x, unsigned char y)
{
    OLED_WrCmd(0xb0+y);
    OLED_WrCmd(((x&0xf0)>>4)+0x10+2);
    OLED_WrCmd((x&0x0f)|0x00);
}

/*********************OLEDè??á************************************/
void OLED_Fill(unsigned char bmp_dat)
{
    unsigned char y,x;
    for(y=0;y<4;y++)
    {
        OLED_WrCmd(0xb0+y);
        OLED_WrCmd(0x00);
        OLED_WrCmd(0x12);
        for(x=0;x<64;x++)
        OLED_WrDat(bmp_dat);
    }
}
/*********************OLED?′??************************************/
void OLED_CLS(void)
{
    unsigned char y,x;
    for(y=0;y<4;y++)
    {
        OLED_WrCmd(0xb0+y);
        OLED_WrCmd(0x00);
        OLED_WrCmd(0x12);
        for(x=0;x<64;x++)
        OLED_WrDat(0);
    }
}
/*********************OLED3?ê??ˉ************************************/
void oled_init(void)
{

    OLED_CS_Clr();
    DELAY_100MS;
    OLED_CS_Set();
    DELAY_100MS;

   OLED_WrCmd(0xAE);     //Set Display Off

   OLED_WrCmd(0xd5);     //display divide ratio/osc. freq. mode
   OLED_WrCmd(0xd1);     //105Hz      内部晶振470 KHz

   OLED_WrCmd(0xA8);     //multiplex ration mode:31
   OLED_WrCmd(0x1F);        //32

   OLED_WrCmd(0xD3);     //Set Display Offset
   OLED_WrCmd(0x00);

   OLED_WrCmd(0x40);     //Set Display Start Line

   OLED_WrCmd(0x8D);     //Set Display Offset
   //OLED_WrCmd(0x10);      //disable charge pump
   OLED_WrCmd(0x14);    //for enabling charge pump

   OLED_WrCmd(0xA1);     //Segment Remap

   OLED_WrCmd(0xC8);     //Sst COM Output Scan Direction

   OLED_WrCmd(0xDA);     //common pads hardware: alternative
   OLED_WrCmd(0x12);

   OLED_WrCmd(0x81);     //contrast control
   OLED_WrCmd(0xCF);


   OLED_WrCmd(0xD9);        //set pre-charge period
   OLED_WrCmd(0xF1);

   OLED_WrCmd(0xDB);     //VCOM deselect level mode
   OLED_WrCmd(0x40);        //set Vvcomh=0.83*Vcc

   OLED_WrCmd(0xA4);     //Set Entire Display On/Off

   OLED_WrCmd(0xA6);     //Set Normal Display

   OLED_WrCmd(0xAF);     //Set Display On

    OLED_Fill(0x00);     //3?ê????á
//  OLED_Set_Pos(0,0);
}

/***************1|?ü?èê?￡o??ê?6*8ò?×é±ê×?ASCII×?·?′?  ??ê?μ?×?±ê￡¨x,y￡?￡?y?aò3·??§0??7****************/
void oled_p6x8_str(unsigned char x, unsigned char y,unsigned char ch[])
{
    unsigned char c=0,i=0,j=0;
    while (ch[j]!='\0')
    {
        c =ch[j]-32;
        if(x>126){x=0;y++;}
        OLED_Set_Pos(x,y);
        for(i=0;i<6;i++)
        OLED_WrDat(F6x8[c][i]);
        x+=6;
        j++;
    }
}
/*******************1|?ü?èê?￡o??ê?8*16ò?×é±ê×?ASCII×?·?′?  ??ê?μ?×?±ê￡¨x,y￡?￡?y?aò3·??§0??7****************/
void oled_p8x16_str(unsigned char x, unsigned char y,unsigned char ch[])
{
    unsigned char c=0,i=0,j=0;
    while (ch[j]!='\0')
    {
        c =ch[j]-32;
        if(x>120){x=0;y++;}
        OLED_Set_Pos(x,y);
        for(i=0;i<8;i++)
        OLED_WrDat(F8X16[c*16+i]);
        OLED_Set_Pos(x,y+1);
        for(i=0;i<8;i++)
        OLED_WrDat(F8X16[c*16+i+8]);
        x+=8;
        j++;
    }
}




void oled_wakeup()
{

    OLED_WrCmd(0xAF);
    DELAY_100MS;
}

void oled_sleep()
{
    OLED_WrCmd(0xAE);
    DELAY_100MS;
}


void oled_show_test()
{

    OLED_Set_Pos(0,0);
    OLED_WrDat(0x00);
    OLED_WrDat(0x60);
    OLED_WrDat(0x18);
    OLED_WrDat(0x16);
    OLED_WrDat(0x11);
    OLED_WrDat(0x16);
    OLED_WrDat(0x18);
    OLED_WrDat(0x60);
    OLED_WrDat(0x00);
    OLED_WrDat(0x00);
    OLED_Set_Pos(0,2);
    OLED_WrDat(0x00);
    OLED_WrDat(0x00);
    OLED_WrDat(0xC0);
    OLED_WrDat(0x38);
    OLED_WrDat(0xE0);
    OLED_WrDat(0x16);
    OLED_WrDat(0x00);
    OLED_WrDat(0x00);
    OLED_WrDat(0x00);
    OLED_WrDat(0x20);
    OLED_WrDat(0x3C);
    OLED_WrDat(0x23);
    OLED_WrDat(0x02);
    OLED_WrDat(0x02);
    OLED_WrDat(0x27);
    OLED_WrDat(0x38);
    OLED_WrDat(0x20);

}
